package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.SolverDao;
import org.elepower.pojo.PsInspectorDetails;
import org.elepower.pojo.PsSolverDetail;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/solver")
public class SolverController {
    @Autowired
    private SolverDao solverDao;

    @RequestMapping("/getSolver")
    @ResponseBody
    public List<PsSolverDetail> getSolver() throws Exception{
        return solverDao.getNormal();
    }

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/solver/index";
    }

    @RequestMapping("/form")
    public String form() throws Exception{
        return "system/solver/form";
    }
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, PsSolverDetail condition, HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<>();

        Page<PsSolverDetail> pager = PageHelper.startPage(page, limit);
        List<PsSolverDetail> list = solverDao.getlist(condition);
        request.setAttribute("list",list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", pager.getTotal());
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(PsSolverDetail psSolverDetail, HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        PsUser user = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        psSolverDetail.setCreatedBy(user.getUsername());
        //添加巡检员信息
        solverDao.add(psSolverDetail);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsSolverDetail psSolverDetail) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //修改巡检员信息
        solverDao.update(psSolverDetail);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public PsSolverDetail get(Integer id) throws Exception{
        return solverDao.get(id);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //删除巡检员
        solverDao.del(id);
        map.put("result",true);
        return map;
    }
}
